<template>
  <div class="back_img">
    <div>
      <p class="title">登录</p>
      <van-image
        class="img_header"
        src="static/head_img.png"
      />
      <van-form class="form">
        <van-field
          v-model="username"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          style="padding-top: 20px"
          v-model="password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <div style="margin: 16px;">
          <van-button round block type="info" native-type="submit" @click="onSubmit">登录</van-button>
        </div>
        <div class="reg">
          <div @click="toRegister">没有账号？立即注册</div>
        </div>
      </van-form>
    </div>

  </div>
</template>

<script>
export default {


  data() {
    return {
      username: '',
      password: '',
    };
  },
  methods: {
    toRegister() {
      this.$router.push('/register');
    },
    onSubmit() {
      var map = {}
      map["username"] = this.username
      map["password"] = this.password

      // console.log(JSON.stringify(map))

      this.axios({
        method: "post",
        url: "/login",
        data: JSON.stringify(map),
        headers: {
          'Content-Type': 'application/json;charset=UTF-8'
        }
      }).then(resp => {
        if (resp) {
          if (resp.data.code == 200) {
            // 成功通知
            this.$notify({type: 'success', message: '登录成功 😊'});
            console.log(resp.data.data.user)
            this.$store.commit('SET_USERINFO', resp.data.data.user)

            this.$store.commit('SET_TOKEN', resp.data.data.token)


            console.log(this.$store.state.token)


            this.$router.replace('/');
          }
        }
      })
    },
  },
};
</script>

<style scoped>
.title {
  /* border-radius: 15px; */
  size: 1px;
  height: 50px;
  line-height: 50px;
  background-color: #20a0ff;
  margin: 0;
  color: #fff;
  text-align: center;
}

.img_header {
  margin: 20px;
}

.form {
  /*margin: 8px;*/
  padding-top: 50px;
  margin: 20px 8px 0 8px;
}

.back_img {
  background-image: url();

}
</style>
